{% extends "base.html" %}
{% load i18n %}
{% load static %}
{% load inventree_extras %}

{% block page_title %}
{% inventree_title %} | {% trans "Index" %}
{% endblock %}

{% block breadcrumb_list %}
{% endblock %}

{% block sidebar %}
<!-- Sidebar data is filled dynamically for the index page-->
{% endblock %}

{% block content %}

<div id='detail-panels'>
</div>

{% endblock %}

{% block js_ready %}

{{ block.super }}

function addHeaderTitle(title) {

    addSidebarHeader({
        text: title,
    });
}

function addHeaderAction(label, title, icon, options) {

    // Construct a "badge" to add to the sidebar item
    var badge = `
    <span id='sidebar-badge-${label}' class='sidebar-item-badge badge rounded-pill badge-right bg-dark'>
        <span class='fas fa-spin fa-spinner'></span>
    </span>
    `;

    addSidebarItem({
        label: label,
        text: title,
        icon: icon,
        content_after: badge
    });

    // Add a detail item to the detail item-panel
    $("#detail-panels").append(
        `<div class='panel panel-inventree panel-hidden' id='panel-${label}'>
            <div class='panel-heading'>
                <h4>${title}</h4>
            </div>
            <div class='panel-content'>
                <table class='table table-condensed table-striped' id='table-${label}'></table>
            </div>
        </div>`
    );

    // Connect a callback to the table
    $(`#table-${label}`).on('load-success.bs.table', function() {
        var count = $(`#table-${label}`).bootstrapTable('getData').length;

        var badge = $(`#sidebar-badge-${label}`);

        badge.html(count);

        if (count > 0) {
            badge.removeClass('bg-dark');
            badge.addClass('bg-primary');
        }
    });
}

{% settings_value 'HOMEPAGE_PART_STARRED' user=request.user as setting_part_starred %}
{% settings_value 'HOMEPAGE_CATEGORY_STARRED' user=request.user as setting_category_starred %}
{% settings_value 'HOMEPAGE_PART_LATEST' user=request.user as setting_part_latest %}
{% settings_value 'HOMEPAGE_BOM_REQUIRES_VALIDATION' user=request.user as setting_bom_validation %}
{% to_list setting_part_starred setting_part_latest setting_bom_validation as settings_list_part %}

{% if roles.part.view and True in settings_list_part %}
addHeaderTitle('{% trans "Parts" %}');

{% if setting_part_starred %}
addHeaderAction('starred-parts', '{% trans "Subscribed Parts" %}', 'fa-bell');
loadSimplePartTable("#table-starred-parts", "{% url 'api-part-list' %}", {
    params: {
        starred: true,
    },
    name: 'starred_parts',
});
{% endif %}

{% if setting_category_starred %}
addHeaderAction('starred-categories', '{% trans "Subscribed Categories" %}', 'fa-bell');
loadPartCategoryTable($('#table-starred-categories'), {
    params: {
        starred: true,
    },
    name: 'starred_categories'
});
{% endif %}

{% if setting_part_latest %}
addHeaderAction('latest-parts', '{% trans "Latest Parts" %}', 'fa-newspaper');
loadSimplePartTable("#table-latest-parts", "{% url 'api-part-list' %}", {
    params: {
        ordering: "-creation_date",
        limit: {% settings_value "PART_RECENT_COUNT" user=request.user %},
    },
    name: 'latest_parts',
});
{% endif %}

{% if setting_bom_validation %}
addHeaderAction('bom-validation', '{% trans "BOM Waiting Validation" %}', 'fa-times-circle');
loadSimplePartTable("#table-bom-validation", "{% url 'api-part-list' %}", {
    params: {
        "bom_valid": false,
    },
    name: 'bom_invalid_parts',
});
{% endif %}
{% endif %}

{% settings_value 'HOMEPAGE_STOCK_RECENT' user=request.user as setting_stock_recent %}
{% settings_value 'HOMEPAGE_STOCK_LOW' user=request.user as setting_stock_low %}
{% settings_value 'HOMEPAGE_SHOW_STOCK_DEPLETED' user=request.user as setting_stock_depleted %}
{% settings_value 'HOMEPAGE_BUILD_STOCK_NEEDED' user=request.user as setting_stock_needed %}
{% settings_value "STOCK_ENABLE_EXPIRY" as expiry %}
{% if expiry %}
{% settings_value 'HOMEPAGE_STOCK_EXPIRED' user=request.user as setting_stock_expired %}
{% settings_value 'HOMEPAGE_STOCK_STALE' user=request.user as setting_stock_stale %}
{% to_list setting_stock_recent setting_stock_low setting_stock_depleted setting_stock_needed setting_stock_expired setting_stock_stale as settings_list_stock %}
{% else %}
{% to_list setting_stock_recent setting_stock_low setting_stock_depleted setting_stock_needed as settings_list_stock %}
{% endif %}

{% if roles.stock.view %}

{% if setting_stock_recent %}
addHeaderAction('recently-updated-stock', '{% trans "Recently Updated" %}', 'fa-clock');
loadStockTable($('#table-recently-updated-stock'), {
    params: {
        part_detail: true,
        ordering: "-updated",
        limit: {% settings_value "STOCK_RECENT_COUNT" user=request.user %},
    },
    name: 'recently-updated-stock',
    grouping: false,
});
{% endif %}

{% if setting_stock_low %}
addHeaderAction('low-stock', '{% trans "Low Stock" %}', 'fa-flag');
loadSimplePartTable("#table-low-stock", "{% url 'api-part-list' %}", {
    params: {
        low_stock: true,
    },
    name: "low_stock_parts",
});
{% endif %}

{% if setting_stock_depleted %}
addHeaderAction('depleted-stock', '{% trans "Depleted Stock" %}', 'fa-times');
loadSimplePartTable("#table-depleted-stock", "{% url 'api-part-list' %}", {
    params: {
        depleted_stock: true,
    },
    name: "depleted_stock_parts",
});
{% endif %}

{% if setting_stock_needed %}
addHeaderAction('stock-to-build', '{% trans "Required for Build Orders" %}', 'fa-bullhorn');
loadSimplePartTable("#table-stock-to-build", "{% url 'api-part-list' %}", {
    params: {
        stock_to_build: true,
    },
    name: "to_build_parts",
});
{% endif %}


{% if expiry %}

{% if setting_stock_expired %}
addHeaderAction('expired-stock', '{% trans "Expired Stock" %}', 'fa-calendar-times');
loadStockTable($("#table-expired-stock"), {
    params: {
        expired: true,
        location_detail: true,
        part_detail: true,
    },
});
{% endif %}

{% if setting_stock_stale %}
addHeaderAction('stale-stock', '{% trans "Stale Stock" %}', 'fa-stopwatch');
loadStockTable($("#table-stale-stock"), {
    params: {
        stale: true,
        expired: false,
        location_detail: true,
        part_detail: true,
    },
});
{% endif %}

{% endif %}
{% endif %}

{% settings_value 'HOMEPAGE_BUILD_PENDING' user=request.user as setting_build_pending %}
{% settings_value 'HOMEPAGE_BUILD_OVERDUE' user=request.user as setting_build_overdue %}
{% to_list setting_build_pending setting_build_overdue as settings_list_build %}

{% if roles.build.view and True in settings_list_build %}
addHeaderTitle('{% trans "Build Orders" %}');

{% if setting_build_pending %}
addHeaderAction('build-pending', '{% trans "Build Orders In Progress" %}', 'fa-cogs');
loadBuildTable("#table-build-pending", {
    locale: '{{ request.LANGUAGE_CODE }}',
    params: {
        active: true,
    },
    disableFilters: true,
});
{% endif %}

{% if setting_build_overdue %}
addHeaderAction('build-overdue', '{% trans "Overdue Build Orders" %}', 'fa-calendar-times');
loadBuildTable("#table-build-overdue", {
    locale: '{{ request.LANGUAGE_CODE }}',
    params: {
        overdue: true,
    },
    disableFilters: true,
});
{% endif %}

{% endif %}

{% settings_value 'HOMEPAGE_PO_OUTSTANDING' user=request.user as setting_po_outstanding %}
{% settings_value 'HOMEPAGE_PO_OVERDUE' user=request.user as setting_po_overdue %}
{% to_list setting_po_outstanding setting_po_overdue as settings_list_po %}

{% if roles.purchase_order.view and True in settings_list_po %}
addHeaderTitle('{% trans "Purchase Orders" %}');

{% if setting_po_outstanding %}
addHeaderAction('po-outstanding', '{% trans "Outstanding Purchase Orders" %}', 'fa-sign-in-alt');
loadPurchaseOrderTable("#table-po-outstanding", {
    url: "{% url 'api-po-list' %}",
    params: {
        supplier_detail: true,
        outstanding: true,
    }
});
{% endif %}

{% if setting_po_overdue %}
addHeaderAction('po-overdue', '{% trans "Overdue Purchase Orders" %}', 'fa-calendar-times');
loadPurchaseOrderTable("#table-po-overdue", {
    url: "{% url 'api-po-list' %}",
    params: {
        supplier_detail: true,
        overdue: true,
    }
});
{% endif %}

{% endif %}

{% settings_value 'HOMEPAGE_SO_OUTSTANDING' user=request.user as setting_so_outstanding %}
{% settings_value 'HOMEPAGE_SO_OVERDUE' user=request.user as setting_so_overdue %}
{% to_list setting_so_outstanding setting_so_overdue as settings_list_so %}

{% if roles.sales_order.view and True in settings_list_so %}
addHeaderTitle('{% trans "Sales Orders" %}');

{% if setting_so_outstanding %}
addHeaderAction('so-outstanding', '{% trans "Outstanding Sales Orders" %}', 'fa-sign-out-alt');
loadSalesOrderTable("#table-so-outstanding", {
    url: "{% url 'api-so-list' %}",
    params: {
        customer_detail: true,
        outstanding: true,
    },
});
{% endif %}

{% if setting_so_overdue %}
addHeaderAction('so-overdue', '{% trans "Overdue Sales Orders" %}', 'fa-calendar-times');
loadSalesOrderTable("#table-so-overdue", {
    url: "{% url 'api-so-list' %}",
    params: {
        overdue: true,
        customer_detail: true,
    }
});
{% endif %}

{% endif %}

{% settings_value 'HOMEPAGE_NEWS' user=request.user as setting_news %}
{% if setting_news and user.is_staff %}

addHeaderTitle('{% trans "InvenTree News" %}');

addHeaderAction('news', '{% trans "Current News" %}', 'fa-newspaper');
loadNewsFeedTable("#table-news", {
    url: "{% url 'api-news-list' %}",
});
{% endif %}

enableSidebar(
    'index',
    {
        hide_toggle: true,
    }
);

{% endblock %}
